﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Data.SqlClient;

namespace PRO_DOCS.BLL.WIN
{

    public class ConnectionStrings
    {
        public void updateConfigFile(string con)
        {
            //updating config file
            XmlDocument XmlDoc = new XmlDocument();
            //Loading the Config file
            XmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
            foreach (XmlElement xElement in XmlDoc.DocumentElement)
            {
                if (xElement.Name == "connectionStrings")
                {
                    //setting the coonection string
                    xElement.LastChild.Attributes[1].Value = con;
                }
            }
            //writing the connection string in config file
            XmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
        }

        public bool UPDATE_CONNECTION_STRING()
        {
            bool cONNECTION_ACCEPTED = false;
            string cONNECTION_STRING = string.Empty;

            {
                ADODB.Connection adodbConnection =
            new ADODB.Connection();
                object connection = (object)adodbConnection;


                MSDASC.DataLinks dlg = new MSDASC.DataLinks();
                dlg.PromptEdit(ref connection);

                string extractedConnection = adodbConnection.ConnectionString;
                if (!string.IsNullOrEmpty((extractedConnection)))
                {

                    List<string> infos = extractedConnection.Split(';').ToList();
                    string server = ".";
                    string database = string.Empty;
                    string userName = string.Empty;
                    string password = string.Empty;

                    for (int i = 0; i < infos.Count; i++)
                    {
                        string value = infos[i].Split('=')[1];
                        if (infos[i].Contains("Password="))
                        {
                            password = value;

                        }
                        if (infos[i].Contains("User ID="))
                        {
                            userName = value;

                        }
                        if (infos[i].Contains("Initial Catalog="))
                        {
                            database = value;

                        }
                        if (infos[i].Contains("Data Source="))
                        {
                            server = value;
                        }
                    }
                    if (extractedConnection.Contains("Persist Security Info=False"))// login
                    {
                        cONNECTION_STRING = string.Format(Properties.Settings.Default.SQL_CONNECTION_WITHOUT_LOGIN, server, database);
                    }
                    else
                    {
                        cONNECTION_STRING = string.Format(Properties.Settings.Default.SQL_CONNECTION_WITH_LOGIN, server, database, userName, password);
                    }
                }
                else
                {
                    cONNECTION_ACCEPTED = false;
                }
            }
            try
            {
                new SqlConnection(cONNECTION_STRING).Open();
                Properties.Settings.Default.PRO_DOCSConnectionString = cONNECTION_STRING;
                Properties.Settings.Default.Save();

                DAL.Properties.Settings.Default.PRO_DOCSConnectionString = cONNECTION_STRING;
                DAL.Properties.Settings.Default.Save();                
                //updateConfigFile(cONNECTION_STRING);

                
                cONNECTION_ACCEPTED = true;
            }
            catch
            {
                cONNECTION_ACCEPTED = false;
            }

            return cONNECTION_ACCEPTED;
        }
    }
}
